package cn.wiseda.finance.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.wiseda.finance.entity.Bank;
import cn.wiseda.finance.entity.Organization;
import cn.wiseda.finance.services.BankServices;
import cn.wiseda.finance.services.OrganServices;

@Controller
@RequestMapping("bank")
public class BankController {

	private static final Logger log = LoggerFactory.getLogger("BankController");

	@Resource
	private BankServices bankServices;

	@Resource
	private OrganServices organServices;

	@RequestMapping(value = "list.jhtml", method = RequestMethod.GET)
	public String bankIndex(@RequestParam(required = false, defaultValue = "1") int organId, Model model) {
		// List<Bank> banks = bankServices.getBanks();
		List<Bank> banks = bankServices.getBanksByOid(organId);

		double sum = bankServices.sumBankRemarksByOid(organId);
		List<Organization> organList = organServices.getOrganList();
		model.addAttribute("organ", organList);
		model.addAttribute("sum", sum);
		model.addAttribute("banks", banks);
		model.addAttribute("oid", organId);
		return "sysconfig/bank";
	}

	@RequestMapping("export.jhtml")
	public void export(HttpServletResponse response) {
		List<Bank> banks = bankServices.getBanks();
		String[] title = new String[] { "ID", "银行名称", "账户类型", "银行帐号", "账户余额", "备注" };
		ExportExcel.exportExcel("banks.xls", title, banks, response);
	}

	@RequestMapping("banks.jhtml")
	public String bankIndex(Bank bank, Model model) {
		List<Bank> banks = bankServices.queryBanks(bank);
		double sum = bankServices.sumBankRemarks();

		model.addAttribute("sum", sum);
		model.addAttribute("banks", banks);

		return "sysconfig/bank";
	}

	@RequestMapping("create.jhtml")
	public String createBank(Model model) {
		List<Organization> organList = organServices.getOrganList();
		model.addAttribute("organ", organList);
		return "sysconfig/create";
	}

	@RequestMapping("saveBank.jhtml")
	public String createBank(Bank bank, Model model) {
		System.out.println(bank);
		boolean flag = bankServices.createBank(bank);
		if (flag) {
			model.addAttribute("isHide", "none");
			return "redirect:list.jhtml";
		}
		model.addAttribute("error", "<div class=\"alert alert-danger\"><strong>错误!</strong>添加银行信息失败！</div>");
		return "sysconfig/bank";
	}

	@RequestMapping("getbank.jhtml")
	public String getBank(int id, Model model) {
		Bank bank = bankServices.getBank(id);
		List<Organization> organList = organServices.getOrganList();
		model.addAttribute("organ", organList);
		model.addAttribute("bank", bank);
		return "sysconfig/modify";
	}

	@RequestMapping("modify.jhtml")
	public String modifyBank(Bank bank, Model model) {
		try {
			boolean flag = bankServices.modifyBank(bank);
			if (flag) {
				model.addAttribute("isHide", "none");
				return "redirect:list.jhtml";
			}
		} catch (Exception e) {
			log.error("修改错误！", e);
			model.addAttribute("error", "<div class=\"alert alert-danger\"><strong>错误!</strong>修改银行信息失败！</div>");
			return "sysconfig/bank";
		}
		model.addAttribute("error", "<div class=\"alert alert-danger\"><strong>错误!</strong>修改银行信息失败！</div>");
		return "sysconfig/bank";

	}

}
